package com.personnel_management.controller;

import com.personnel_management.pojo.Announcement;
import com.personnel_management.service.AnnounService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("announ")
public class AnnounController {
    @Autowired
    private AnnounService service;
    //添加公告
    @CrossOrigin("*")
    @RequestMapping(value = "add")
    @ResponseBody
    public Integer addAnnoun(Announcement announcement/*@RequestParam String title,@RequestParam String content*/)
    {
        /*Announcement announcement=new Announcement();
        announcement.setTitle(title);
        announcement.setContent(content);*/
       return service.addAnnoun(announcement);
       //return announcement.toString();
    }
    //查找所有公告
    //@CrossOrigin("*")
    /*@RequestMapping("find")
    @ResponseBody
    //code 0 msg:成功 data
    public List<Announcement> getAnnouns()
    {
        return service.getAnnouns();
    }*/
    @CrossOrigin("*")
    @RequestMapping("find")
    public ModelAndView mv1()
    {
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        List<Announcement> announs = service.getAnnouns();
        mv.addObject("code","0");
        mv.addObject("msg","");
        mv.addObject("data",announs);
        return mv;

    }

    //根据id删除公告
    @CrossOrigin("*")
    @RequestMapping("delete")
    @ResponseBody
    public Integer deleteAnnoun(int id)
    {
        return service.deleteAnnoun(id);
    }
    //根据id修改公告的内容、标题、内容和标题
    @CrossOrigin("*")
    @RequestMapping("set")
    @ResponseBody
    public Integer setAnnounByid(Announcement a)
    {
        return service.setAnnounByid(a);
    }
    //测试trim函数
    @CrossOrigin(origins = "*")
    @RequestMapping("/trim")
    @ResponseBody
    public String test(String str)
    {
        //需求:删掉末尾的逗号(如果有的话)
        if(str.endsWith(","))
            return str.substring(0,str.length()-1);
        else
            return str;

    }
}
